package routers

import (
	"gin-demo/controllers/admin"
	_case "gin-demo/controllers/v1/case"
	"gin-demo/middlewares"
	"github.com/gin-gonic/gin"
	"time"
)

// 路由初始化
func ApiRouterInit(r *gin.Engine) {
	apiRouter := r.Group("/api") //这里可以写公共的中间件，可在r.Group("/api", 中间件 ...)，也可另起一行 使用 r.use(中间件 ...)
	apiRouter.Use(middlewares.RateLimitMiddleware(time.Second, 100, 100))
	ApiRouterNoNeedAuth(apiRouter)
	ApiRouterNeedAuth(apiRouter)
}

// 定义需要登录校验的路由
func ApiRouterNeedAuth(app *gin.RouterGroup) {
	app.Use(middlewares.JwtAuthMiddleware())
	app.GET("/user", admin.UserController{}.Index)
	app.POST("/user", admin.UserController{}.Add)
	app.PUT("/user:uid", admin.UserController{}.Edit)
}

// 定义不需要登录校验的路由
func ApiRouterNoNeedAuth(app *gin.RouterGroup) {
	app.Use(middlewares.CheckAuthMiddleware())
	app.POST("/login", middlewares.AuthHandler)
	app.POST("/index", admin.IndexController{}.Index)
	app.GET("/case/label_log/", _case.CaseLabelLogController{}.List)
}
